package com.aidex.common.core.domain.entity;

import com.aidex.common.annotation.Excel;
import com.aidex.common.annotation.Excel.Type;
import com.aidex.common.annotation.Excels;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.xss.Xss;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
@Api(tags = "用户对象")
public class SysUser extends BaseEntity {

    @ApiModelProperty("部门ID")
    @Excel(name = "部门ID", type = Type.IMPORT)
    private String deptId;

    @ApiModelProperty("姓名")
    @Excel(name = "姓名")
    private String name;

    @ApiModelProperty("英文名称")
    @Excel(name = "英文名称")
    private String nameEn;

    @ApiModelProperty("用户编号")
    @Excel(name = "用户编号")
    private String no;

    @ApiModelProperty("登录名")
    @Excel(name = "登录名")
    @NotBlank(message = "登录名不能为空")
    @Size(max = 30, message = "登录名长度不能超过30个字符")
    @Xss(message = "用登录名不能包含脚本字符")
    private String userName;

    @ApiModelProperty("别称")
    @Excel(name = "别称")
    @Size(max = 30, message = "用户昵称长度不能超过30个字符")
    @Xss(message = "用户账号不能包含脚本字符")
    private String nickName;

    @ApiModelProperty("用户类型")
    @Excel(name = "用户类型", dictType = "sys_user_type")
    private String userType;

    @ApiModelProperty("用户邮箱")
    @Excel(name = "用户邮箱")
    @Email(message = "邮箱格式不正确")
    @Size(max = 50, message = "邮箱长度不能超过50个字符")
    private String email;

    @ApiModelProperty("手机号码")
    @Excel(name = "手机号码")
    @Size(max = 11, message = "手机号码长度不能超过11个字符")
    private String phonenumber;

    @ApiModelProperty("用户性别")
    @Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
    private String sex;

    @ApiModelProperty("用户头像")
    private String avatar;

    @ApiModelProperty("密码")
    @JsonIgnore
    @JsonProperty
    private String password;

    @ApiModelProperty("生日")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date birthday;

    @ApiModelProperty("民族")
    private String nation;

    @ApiModelProperty("籍贯")
    private String birthAddress;

    @ApiModelProperty("政治面貌")
    private String polity;

    @ApiModelProperty("职称")
    private String title;

    @ApiModelProperty("办公电话")
    private String officeTel;

    @ApiModelProperty("传真号")
    private String fax;

    @ApiModelProperty("工作地点")
    private String workSpace;

    @ApiModelProperty("排序号")
    private Integer sort;

    @ApiModelProperty("工作地点")
    private String userPinyin;

    @ApiModelProperty("帐号状态")
    @Excel(name = "帐号状态", readConverterExp = "0=正常,1=停用")
    private String status;

    @ApiModelProperty("最后登录IP")
    @Excel(name = "最后登录IP", type = Type.EXPORT)
    private String loginIp;

    @ApiModelProperty("最后登录时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "最后登录时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Type.EXPORT)
    private Date loginDate;

    @ApiModelProperty("部门对象")
    @Excels({
        @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
        @Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT)
    })
    @TableField(exist = false)
    private SysDept dept;

    @ApiModelProperty("角色组对象")
    @Excels({
            @Excel(name = "角色名称", targetAttr = "roleName", type = Type.EXPORT),
            @Excel(name = "角色编码", targetAttr = "roleKey", type = Type.EXPORT)
    })
    @TableField(exist = false)
    private List<SysRole> roles;

    @ApiModelProperty("岗位组对象")
    @Excels({
            @Excel(name = "岗位名称", targetAttr = "postName", type = Type.EXPORT),
            @Excel(name = "岗位编码", targetAttr = "postCode", type = Type.EXPORT)
    })
    @TableField(exist = false)
    private List<SysPost> posts;

    @ApiModelProperty("角色组")
    @TableField(exist = false)
    private String[] roleIds;

    @ApiModelProperty("岗位组")
    @TableField(exist = false)
    private String[] postIds;

    @TableField(exist = false)
    private boolean admin;

    public SysUser() {
    }

    public SysUser(String id) {
        setId(id);
    }

    public boolean isAdmin() {
        return SysUser.isAdmin(getId());
    }

    public static boolean isAdmin(String id) {
        return StringUtils.isNotBlank(id) && "1".equals(id);
    }

    public String[] getRoleIds() {
        if(this.roleIds != null) {
            return this.roleIds;
        }
        List<String> roleIds = new ArrayList<>();
        if (this.roles != null && !this.roles.isEmpty()) {
            for (SysRole role : roles) {
                roleIds.add(role.getId());
            }
        }
        return roleIds.toArray(new String[]{});
    }

    public String[] getPostIds() {
        if(this.postIds != null) {
            return this.postIds;
        }
        List<String> postIds = new ArrayList<>();
        if (this.posts != null && !this.posts.isEmpty()) {
            for (SysPost post : posts) {
                postIds.add(post.getId());
            }
        }
        return postIds.toArray(new String[]{});
    }
}
